package com.area.cth.controller;

import com.area.cth.pojo.Exchange;
import com.area.cth.common.Result;
import com.area.cth.service.ExchangeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "物品置换相关接口")
@RestController
@RequestMapping("/exchange")
@CrossOrigin
@Slf4j
public class ExchangeController {
    @Autowired
    private ExchangeService es;

    @Operation(summary = "新增物品置换")
    @PostMapping
    public Result addExchange(@RequestBody Exchange e) {
        log.info("新增物品置换：{}",e);
        es.addExchange(e);
        return new Result(0,"发布成功",null);
    }

    @Operation(summary = "查询全部物品置换")
    @GetMapping("/getExchange")
    public Result<List<Exchange>> getExchangeList() {
        List<Exchange> exchangeList = es.getExchangeList();
        return Result.success(exchangeList);
    }

    @Operation(summary = "分页查询")
    @GetMapping("/getExchangeList")
    public Result<IPage<Exchange>> exchangeList(Integer pageNo, Integer pageSize, @RequestParam(required = false) String auditState, @RequestParam(required = false) Integer isFree, @RequestParam(required = false) String keyword) {
        if (auditState.equals("")) {
            auditState = null;
        }
        IPage<Exchange> exchangeList = es.exchangeList(pageNo, pageSize, auditState, isFree, keyword);
        return Result.success(exchangeList);
    }

    @Operation(summary = "查询全部免费物品")
    @GetMapping("/getFree")
    public Result<List<Exchange>> getFreeList() {
        List<Exchange> freeList = es.getFreeList();
        return Result.success(freeList);
    }

    @Operation(summary = "查询个人发布的已通过的物品置换")
    @GetMapping("/myExchangeTrue")
    public Result<List<Exchange>> myExchangeTrueList() {
        List<Exchange> myExchangeList = es.getMyExchangeTrueList();
        return Result.success(myExchangeList);
    }

    @Operation(summary = "查询个人发布的审核中的物品置换")
    @GetMapping("/myExchangeAudit")
    public Result<List<Exchange>> myExchangeAuditList() {
        List<Exchange> myExchangeList = es.getMyExchangeAuditList();
        return Result.success(myExchangeList);
    }

    @Operation(summary = "查询个人发布的未通过的物品置换")
    @GetMapping("/myExchangeFalse")
    public Result<List<Exchange>> myExchangeFalseList() {
        List<Exchange> myExchangeList = es.getMyExchangeFalseList();
        return Result.success(myExchangeList);
    }

    @Operation(summary = "根据发布者id查询已通过的物品置换")
    @GetMapping("/getExchangeByUser")
    public Result<List<Exchange>> getExchangeTrueByUserList(Integer userId) {
        List<Exchange> getExchangeByUserList = es.getExchangeTrueByUserList(userId);
        return Result.success(getExchangeByUserList);
    }

    @Operation(summary = "根据id查询物品置换")
    @GetMapping("/getById")
    public Result<Exchange> exchangeById(Integer id) {
        Exchange exchangeById = es.getExchangeById(id);
        return Result.success(exchangeById);
    }

    @Operation(summary = "修改物品置换")
    @PutMapping("/update")
    public Result updateExchange(@RequestBody @Validated Exchange e) {
        es.update(e);
        return new Result(0,"修改成功",null);
    }

    @Operation(summary = "修改物品置换审核状态")
    @PutMapping
    public Result updateExchangeAudit(String audit, Integer id) {
        es.updateAudit(audit, id);
        return new Result(0,"修改成功",null);
    }

    @Operation(summary = "删除物品置换")
    @DeleteMapping
    public Result deleteExchange(Integer id) {
        es.delete(id);
        return new Result(0,"下架成功",null);
    }
}
